// productGroup.js

var requests = require('../../requests/api.js');
import util from '../../utils/util.js'

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //没有更多
    loading: true, //加载框是否显示
    searchLoading: false, //把"上拉加载"的变量设为true，显示  
    searchLoadingComplete: false, //把“没有数据”设为false，隐藏  
    page: 1, //分页数据，从第一页开始
    goods: [],

    groupInfo: null,
    groupId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    new app.WeToast();
    //兼容老版本。之前的topic访问专题详情。group是新的产品组
    if (options.groupId) {
      console.log(options.groupId)
      this.setData({
        groupId: options.groupId
      })
      this.get_group();
      this.get_groupProduct(this.data.page);
    }else {
      console.log(options.topicId)
      this.setData({
        groupId: options.topicId
      })
      this.get_topic();
      this.get_topicProduct(this.data.page);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    let groupName = this.data.groupInfo.name
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: groupName,
      path: '/pages/topic/productGroup?groupId=' + this.data.groupInfo.id,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },


  //NET
  //产品组信息
  get_group: function() {
    const that = this
    var params = that.data.groupId
    requests.get_group(params, (res) => {

      that.setData({
        groupInfo: res.data
      })
      let groupName = this.data.groupInfo.name
      wx.setNavigationBarTitle({
        title: groupName
      })
    }, (data) => {
      if (util.alertNetworkError(data)) {
        that.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {});
  },

  //产品组产品
  get_groupProduct: function(page) {
    //网络请求的时候先把加载框加出来
    const self = this;
    self.setData({
      loading: false,
    });

    var params = {
      groupId: this.data.groupId,
      currentPage: page,
      pageSize: 10
    };

    requests.get_groupProduct(params, (res) => {
      if (!res.data.products) return;
      if (res.data.products.length == 0) { //没有数据
        self.setData({
          searchLoadingComplete: true, //把“没有数据”设为true，显示

        })
      } else { //说明还有数据
        self.setData({
          searchLoadingComplete: false, //把“没有数据”设为false，隐藏  
        })
      };
      self.setData({
        loading: true, //隐藏加载框
        goods: self.data.goods.concat(res.data.products) //给数组赋值
      })

    }, (data) => {
      self.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示
        loading: true //隐藏加载框
      })
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }

    }, () => {
      self.setData({
        searchLoading: false //把"上拉加载"的变量设为false，隐藏  

      })
    });
  },

  //NET
  //专题信息
  get_topic: function () {
    const that = this
    var params = that.data.groupId
    requests.get_topic(params, (res) => {

      that.setData({
        groupInfo: res.data
      })
      let topicName = this.data.groupInfo.topicName
      wx.setNavigationBarTitle({
        title: topicName
      })
    }, (data) => {
      if (util.alertNetworkError(data)) {
        that.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => { });

  },

  //专题产品
  get_topicProduct: function (page) {
    //网络请求的时候先把加载框加出来
    const self = this;
    self.setData({
      loading: false,
    });

    var params = {
      groupId: this.data.groupId,
      currentPage: page,
      pageSize: 10
    };

    requests.get_topicProduct(params, (res) => {
      if (!res.data.products) return;
      if (res.data.products.length == 0) { //没有数据
        self.setData({
          searchLoadingComplete: true, //把“没有数据”设为true，显示

        })
      } else { //说明还有数据
        self.setData({
          searchLoadingComplete: false, //把“没有数据”设为false，隐藏  
        })
      };
      self.setData({
        loading: true, //隐藏加载框
        goods: self.data.goods.concat(res.data.products) //给数组赋值
      })

    }, (data) => {
      self.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示
        loading: true //隐藏加载框
      })
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {
      self.setData({
        searchLoading: false //把"上拉加载"的变量设为false，隐藏  

      })
    });
  },


  /**
   * 商品详情页
   */
  toDetailsTap: function(e) {
    var id = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '../product/productDetail?productId=' + id,
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.moreData()

  },
  //上拉触底
  moreData: function() {
    let that = this;
    //如果还有数据就继续加载
    if (!that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        page: that.data.page + 1
      })
      that.setData({
        searchLoading: true //把"上拉加载"的变量设为false，隐藏

      })

      that.get_groupProduct(that.data.page)
    }
  },
})